﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Colorful;
using Microsoft.AspNetCore.Authorization;
using MongoDB.Driver;
using Colorful.CMS.Core;
using System.Text;
using Colorful.Models;

namespace Colorful.CMS.Controllers
{
    #region HeadViewComponent
    public class HeadViewComponent : ViewComponent
    {
        public IViewComponentResult Invoke()
        {
            var model = new HeadViewModel()
            {
                Description = WebService.WebSetting.Description,
                Keyword = WebService.WebSetting.Keyword
            };
            var url = $"{HttpContext.Request.Path}".ToLower();
            var title = new StringBuilder();
            if (url == "/" || url == "/index")
            {
                if (string.IsNullOrEmpty(WebService.WebSetting.Title))
                    title.Append($"{WebService.WebSetting.SiteName} - {WebService.WebSetting.Keyword}");
                else
                    title.Append(WebService.WebSetting.Title);
            }
            else
            {
                title.Append($"{ViewData["Title"]}");
                switch (WebService.WebSetting.PageTitleShowType)
                {
                    case PageTitleShowType.Keyword:
                        title.Append($" - {WebService.WebSetting.Keyword}");
                        break;
                    case PageTitleShowType.SiteName:
                        title.Append($" - {WebService.WebSetting.SiteName}");
                        break;
                    case PageTitleShowType.KeywordAndSiteName:
                        title.Append($" - {WebService.WebSetting.Keyword} {WebService.WebSetting.SiteName}");
                        break;
                }
            }
            model.Title = title.ToString();
            return View(model);
        }
    }
    #endregion

    #region ScriptViewComponent
    public class ScriptViewComponent : ViewComponent
    {
        public IViewComponentResult Invoke()
        {
            return View();
        }
    }
    #endregion

    #region HeadViewModel
    public class HeadViewModel
    {
        /// <summary>
        /// 网站标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 网站描述
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 网站关键字
        /// </summary>
        public string Keyword { get; set; }
    }
    #endregion
}
